
#ifndef _MEMBER_BSURFACE_H_
#define _MEMBER_BSURFACE_H_

#include <BOctTree>
#include <BSurface>
#include "ITriangleCallback.h"

#define surface_member(surface)		((member_BSurface*)((BSurface*)surface)->_ptr)

namespace BWE
{
	typedef BOctTree<BIntArray>				QuadOctTree;
	typedef BOctTree<BIntArray>::Item			QuadOctTreeItem;
	typedef BOctTree<BIntArray>::ItemArray	QuadOctTreeItemArray;

	class member_BSurface
	{
		friend class BSurface;

		struct Node
		{
			BSpace	space;
			BVector	center;
			int		index = -1;
		};
		typedef BArray<Node> NodeArray;

	public:
		member_BSurface(BSurface* surface);
		~member_BSurface();

		bool rebuildHorizontal();
		bool rebuildVertical();
		void freshOctTree();
		void processTriangles(ITriangleCallback* callback, const BSpace& space) const;

		void build(BMesh* mesh, const BSpace& space);
		void buildTree(int startIndex, int endIndex);

		void overlapping(ITriangleCallback* callback, const BSpace& space) const;
		void overlapping(ITriangleCallback* callback, const BLine& line) const;
		void processNode(ITriangleCallback* callback, int index) const;

		BSurface*		boss;
		BQuad			quad;
		BVectorArray	edges;
		Orientation		orientation;
		int				verticalSegment;
		int				horizontalSegment;
		bool			dirty;

		BHolder<BVectorArray>		vertices;
		BHolder<BVectorArray>		normals;
		BHolder<BColorArray>		colors;
		BHolder<BCoordArray>		coords;
		BHolder<BIntArray>		indices;
		BHolder<QuadOctTree>		octTree;

		BSpace					m_space;
		int						lastIndex;
		NodeArray				leafNodes;
		NodeArray				treeNodes;
		mutable int				m_processCount;

	};
}

#endif